﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_RenYuanQuanXianService : It_RenYuanQuanXianService
    {
        public readonly MyContext _Context;
        public t_RenYuanQuanXianService(MyContext context)
        {
            _Context = context;
        }
        public t_RenYuanQuanXian Add(t_RenYuanQuanXian t_RenYuanQuanXian)
        {
            _Context.t_RenYuanQuanXian.Add(t_RenYuanQuanXian);
            _Context.SaveChanges();
            return _Context.t_RenYuanQuanXian.FirstOrDefault(x => x.BeiZhu1 == t_RenYuanQuanXian.BeiZhu1 && x.BeiZhu2 == t_RenYuanQuanXian.BeiZhu2 && x.BeiZhu3 == t_RenYuanQuanXian.BeiZhu3 && x.BeiZhu4 == t_RenYuanQuanXian.BeiZhu4 && x.BeiZhu5 == t_RenYuanQuanXian.BeiZhu5 && x.RenYuanId == t_RenYuanQuanXian.RenYuanId && x.QuanXianId == t_RenYuanQuanXian.QuanXianId);

        }

        public bool Delete(int id)
        {
            _Context.t_RenYuanQuanXian.Remove(_Context.t_RenYuanQuanXian.Where(x => x.Id == id).ToList()[0]);
            return _Context.SaveChanges() > 0;
        }

        public t_RenYuanQuanXian GetById(int id)
        {
            return _Context.t_RenYuanQuanXian.FirstOrDefault(x => x.Id == id);
        }

        public List<t_RenYuanQuanXian> GetList()
        {
            return _Context.t_RenYuanQuanXian.ToList();
        }

        public List<t_GangWeiBiao> BuMenLianDong(int id) 
        {
            return _Context.t_GangWeiBiao.Where(x=>x.BuMenId==id).ToList();
        }
        public List<t_ZhanDuiBiao> ZhanDuiLianDong(int id)
        {
            return _Context.t_ZhanDuiBiao.Where(x => x.ZhanQuId == id).ToList();
        }

        public string GetListWithFenYe(t_RenYuanBiao t_RenYuanBiao, int start, int limit, string dir, int column)
        {
            var list = _Context.t_RenYuanBiao.Include(x => x.t_GangWeiBiao).Where(ReturnWhere(t_RenYuanBiao)).ToList();
            var listbumen = _Context.t_BuMenBiao.ToList();
            var listgongsi = _Context.t_GongSiBiao.ToList();
            for (int i = 0; i < list.Count; i++)
            {
                list[i].t_GangWeiBiao.t_RenYuanBiaos = null;
                if (list[i].t_ZhanDuiBiao == null)
                {
                    list[i].t_ZhanDuiBiao = new t_ZhanDuiBiao();
                }
                else
                {
                    list[i].t_ZhanDuiBiao.t_RenYuanBiaos = null;
                }
                list[i].t_GangWeiBiao.t_BuMenBiao = listbumen.Where(x => x.Id == list[i].t_GangWeiBiao.BuMenId).ToList()[0];
                list[i].t_GangWeiBiao.t_BuMenBiao.t_GongSiBiao = listgongsi.Where(x => x.Id == list[i].t_GangWeiBiao.t_BuMenBiao.GongSiId).ToList()[0];
            }
            switch (column)
            {
                case 0:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.XingMing).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.XingMing).ToList();
                    }
                    break;
                case 1:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.NianLing).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.NianLing).ToList();
                    }
                    break;
                case 2:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.XingBie).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.XingBie).ToList();
                    }
                    break;
                case 3:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.LianXiFangShi).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.LianXiFangShi).ToList();
                    }
                    break;
                case 4:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.t_GangWeiBiao.t_BuMenBiao.BuMenMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.t_GangWeiBiao.t_BuMenBiao.BuMenMingCheng).ToList();
                    }
                    break;
                case 5:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.t_GangWeiBiao.GangWeiMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.t_GangWeiBiao.GangWeiMingCheng).ToList();
                    }
                    break;
                case 6:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.t_GangWeiBiao.t_BuMenBiao.t_GongSiBiao.GongSiMingCheng).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.t_GangWeiBiao.t_BuMenBiao.t_GongSiBiao.GongSiMingCheng).ToList();
                    }
                    break;
                case 7:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.ShenFenZhengHao).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.ShenFenZhengHao).ToList();
                    }
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }

        public string ReturnWhere(t_RenYuanBiao t_RenYuanBiao)
        {
            string returnstr = "Id != 1";
            if (t_RenYuanBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(\"" + t_RenYuanBiao.BeiZhu1 + "\")";
            }
            if (t_RenYuanBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(\"" + t_RenYuanBiao.BeiZhu2 + "\")";
            }
            if (t_RenYuanBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(\"" + t_RenYuanBiao.BeiZhu3 + "\")";
            }
            if (t_RenYuanBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(\"" + t_RenYuanBiao.BeiZhu4 + "\")";
            }
            if (t_RenYuanBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(\"" + t_RenYuanBiao.BeiZhu5 + "\")";
            }
            if (t_RenYuanBiao.BeiZhu6 != null)
            {
                returnstr = returnstr + " and BeiZhu6.Contains(\"" + t_RenYuanBiao.BeiZhu6 + "\")";
            }
            if (t_RenYuanBiao.BeiZhu7 != null)
            {
                returnstr = returnstr + " and BeiZhu7.Contains(\"" + t_RenYuanBiao.BeiZhu7 + "\")";
            }
            if (t_RenYuanBiao.BeiZhu8 != null)
            {
                returnstr = returnstr + " and BeiZhu8.Contains(\"" + t_RenYuanBiao.BeiZhu8 + "\")";
            }
            //if (t_RenYuanBiao.BeiZhu9 != null)
            //{
            //    returnstr = returnstr + " and BeiZhu9.Contains(\"" + t_RenYuanBiao.BeiZhu9 + "\")";
            //}
            //if (t_RenYuanBiao.BeiZhu10 != null)
            //{
            //    returnstr = returnstr + " and BeiZhu10.Contains(\"" + t_RenYuanBiao.BeiZhu10 + "\")";
            //}
            if (t_RenYuanBiao.XingMing != null)
            {
                returnstr = returnstr + " and XingMing.Contains(\"" + t_RenYuanBiao.XingMing + "\")";
            }
            if (t_RenYuanBiao.ZhangHao != null)
            {
                returnstr = returnstr + " and ZhangHao.Contains(\"" + t_RenYuanBiao.ZhangHao + "\")";
            }
            if (t_RenYuanBiao.MiMa != null)
            {
                returnstr = returnstr + " and MiMa.Contains(\"" + t_RenYuanBiao.MiMa + "\")";
            }
            if (t_RenYuanBiao.NianLing != null)
            {
                returnstr = returnstr + " and NianLing.Contains(\"" + t_RenYuanBiao.NianLing + "\")";
            }
            if (t_RenYuanBiao.XingBie != null)
            {
                returnstr = returnstr + " and XingBie.Contains(\"" + t_RenYuanBiao.XingBie + "\")";
            }
            if (t_RenYuanBiao.LianXiFangShi != null)
            {
                returnstr = returnstr + " and LianXiFangShi.Contains(\"" + t_RenYuanBiao.LianXiFangShi + "\")";
            }
            if (t_RenYuanBiao.GangWeiId > 0)
            {
                returnstr = returnstr + " and GangWeiId == " + t_RenYuanBiao.GangWeiId;
            }
            if (t_RenYuanBiao.ZhuangTai != null)
            {
                returnstr = returnstr + " and ZhuangTai.Contains(\"" + t_RenYuanBiao.ZhuangTai + "\")";
            }
            if (t_RenYuanBiao.XingJi != null)
            {
                returnstr = returnstr + " and XingJi.Contains(\"" + t_RenYuanBiao.XingJi + "\")";
            }
            if (t_RenYuanBiao.ZhanDuiId > 0)
            {
                returnstr = returnstr + " and ZhanDuiId == " + t_RenYuanBiao.ZhanDuiId;
            }
            if (t_RenYuanBiao.HunYinZhuangKuang != null)
            {
                returnstr = returnstr + " and HunYinZhuangKuang.Contains(\"" + t_RenYuanBiao.HunYinZhuangKuang + "\")";
            }
            if (t_RenYuanBiao.WeiXinHao != null)
            {
                returnstr = returnstr + " and WeiXinHao.Contains(\"" + t_RenYuanBiao.WeiXinHao + "\")";
            }
            if (t_RenYuanBiao.YouXiang != null)
            {
                returnstr = returnstr + " and YouXiang.Contains(\"" + t_RenYuanBiao.YouXiang + "\")";
            }
            if (t_RenYuanBiao.ShenFenZhengHao != null)
            {
                returnstr = returnstr + " and ShenFenZhengHao.Contains(\"" + t_RenYuanBiao.ShenFenZhengHao + "\")";
            }
            if (t_RenYuanBiao.JiaTingDiZhi != null)
            {
                returnstr = returnstr + " and JiaTingDiZhi.Contains(\"" + t_RenYuanBiao.JiaTingDiZhi + "\")";
            }
            if (t_RenYuanBiao.XueLi != null)
            {
                returnstr = returnstr + " and XueLi.Contains(\"" + t_RenYuanBiao.XueLi + "\")";
            }
            if (t_RenYuanBiao.RuZhiShiJian != null)
            {
                returnstr = returnstr + " and RuZhiShiJian.Contains(\"" + t_RenYuanBiao.RuZhiShiJian + "\")";
            }
            if (t_RenYuanBiao.LiZhiShiJian != null)
            {
                returnstr = returnstr + " and LiZhiShiJian.Contains(\"" + t_RenYuanBiao.LiZhiShiJian + "\")";
            }
            if (t_RenYuanBiao.BeiZhu10!=null)
            {
                returnstr = returnstr + " and " + t_RenYuanBiao.BeiZhu10;
            }
            if (t_RenYuanBiao.BeiZhu9 != null)
            {
                returnstr = returnstr + " and " + t_RenYuanBiao.BeiZhu9;
            }
            return returnstr;
        }

        public t_RenYuanQuanXian Quanxiid(int id)
        {
            return _Context.t_RenYuanQuanXian.FirstOrDefault(x => x.RenYuanId == id);
        }
    }
}
